/// @description 加载地图
// You can write your code in this editor

#region //加载地图

if(!load)
{
	//清空object
	with(oPlayer)
		instance_destroy(id);
	with(oBoxFa)
		instance_destroy(id);
	with(oPallet)
		instance_destroy(id);
		
	//读取文件
	var map_file = file_text_open_read(map_file_path);
	if(map_file)
	{
		//循环遍历
		var line = 0;
		while(!file_text_eof(map_file))
		{
			line++;
			//读取文件行
			var data = file_text_readln(map_file);
			var length = string_length(data);
			show_debug_message(data);
			
			#region//生成地图
			for(var i=0;i<length;i++)
			{
				switch(string_char_at(data,i))
				{
					//墙壁
					case "#":
						instance_create_depth(x+i*item_width, y+line*item_height, depth, oWall);
					break;
						
					//箱子
					case "$":
						instance_create_depth(x+i*item_width, y+line*item_height, depth, oBox);
					break;
						
					//目标
					case ".":
						instance_create_depth(x+i*item_width, y+line*item_height, depth, oPallet);
					break;
						
					//玩家
					case "@":
						instance_create_depth(x+i*item_width, y+line*item_height, depth, oPlayer);
					break;
				}
			}
			#endregion
		}
	}
	else
	{
		room_goto(rmMenu);
		return;
	}
	file_text_close(map_file);
	load = true;
}
else
{
	if(keyboard_check_pressed(ord("R")))
	{
		with(oPlayer)
			instance_destroy(id);
		with(oBoxFa)
			instance_destroy(id);
		with(oPallet)
			instance_destroy(id);
			
		load = false;
		return;
	}
		
	#region 判断是否过关
	success = true;
	with(oBox)
	{
		if(image_index==0)
			other.success = false;
	}
	
	//下一关
	if(success)
	{
		with(oPlayer)
			instance_destroy(id);
		with(oBoxFa)
			instance_destroy(id);
		with(oPallet)
			instance_destroy(id);
	
		//下一关
		success = false;
		level++;
		//地图文件
		map_file_path = "map/screen."+string(level);
		load = false;
	}
	#endregion
}

#endregion


